using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Collections;
using System.DirectoryServices;
namespace MES.Utility
{
    static class ADAccess
    {
        private static bool Authenticate(string userName,string password, string domain)
        {
            bool authentic = false;
            try
            {
                DirectoryEntry entry = new DirectoryEntry(@"LDAP://" + domain, userName, password);
                object nativeObject = entry.NativeObject;
                authentic = true;
            }
            catch (DirectoryServicesCOMException)
            {
                authentic = false;
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                authentic = false;
            }
            return authentic;
        }
        public static ArrayList AttributeValuesMultiString(string attributeName,string objectDn, ArrayList valuesCollection, bool recursive)
        {
            DirectoryEntry ent = new DirectoryEntry(objectDn);
            PropertyValueCollection ValueCollection = ent.Properties[attributeName];
            IEnumerator en = ValueCollection.GetEnumerator();

            while (en.MoveNext())
            {
                if (en.Current != null)
                {
                    if (!valuesCollection.Contains(en.Current.ToString()))
                    {
                        valuesCollection.Add(en.Current.ToString());
                        if (recursive)
                        {
                            AttributeValuesMultiString(attributeName, @"LDAP://" +
                            en.Current.ToString(), valuesCollection, true);
                        }
                    }
                }
            }
            ent.Close();
            ent.Dispose();
            return valuesCollection;
        }
        public static ArrayList DomainGroups(string name)
        {
            WindowsIdentity wi = null;
            ArrayList ar = null;
            try 
	        {
                wi = new WindowsIdentity(name);
                ar = new ArrayList();
	        }
            catch (Exception)
	        {        		
		        throw;
	        }
            try
            {
                NTAccount account = null;
                foreach (IdentityReference ir in wi.Groups)
                {

                    try
                    {
                        account = (NTAccount)ir.Translate(typeof(NTAccount));
                    }
                    catch (Exception)
                    {                        
                        throw;
                    }
                    if (account != null)
                    {
                        ar.Add(account.Value);
                    }

                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                wi.Dispose();
            }
            return ar;            
        }
        public static ArrayList Groups(string userDn, bool recursive)
        {
            ArrayList groupMemberships = new ArrayList();
            return AttributeValuesMultiString("memberOf", userDn,
                groupMemberships, recursive);
        }
    }
}
